/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mnstudent.businessLayer;

import mnstudent.dataacesslayer.StudentDAL;
import mnstudent.datatranferobject.PriorityObjectDTO;
import mnstudent.datatranferobject.StudentDTO;
import mnstudent.global.Standard;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import mnstudent.datatranferobject.ScoreStudentDTO;
import mnstudent.datatranferobject.StudentDisplayDTO;

/**
 *
 * @author 11520_000
 */
public class StudentBLL {

    StudentDAL studentDAL;

    public StudentBLL() {
        studentDAL = new StudentDAL();
    }

    /**
     * ham lay tat ca cac Sinh Vien
     *
     * @return ResultSet
     */
    public ResultSet getAllInfoStudent() {
        return studentDAL.getAllInfoStudent();
    }

    public ResultSet getAllStudent() {
        return studentDAL.getAllStudent();
    }

    public ResultSet getRegistrationCoursesInforOfStudent(Object[] _value){
        return studentDAL.getRegistrationCoursesInforOfStudent(_value);
    }
   
    public ResultSet getAllPriorityObjectOfStudentByIdFromDatabase(String _id)
    {
        return  studentDAL.getAllPriorityObjectOfStudentByIdFromDatabase(_id);
    }

    public ArrayList<PriorityObjectDTO> getListPriorityObjectOfStudent(String _id) {
        ArrayList<PriorityObjectDTO> temp = new ArrayList<>();
        ResultSet rs = getAllPriorityObjectOfStudentByIdFromDatabase(_id);
        try {
            while (rs.next()) {
                PriorityObjectDTO obj = new PriorityObjectDTO(rs.getNString(1), rs.getNString(2));
                temp.add(obj);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return temp;
    }

    public ResultSet getAllStudentByConditionFromDatabase(Object[] _value){
       return  studentDAL.getAllStudentByConditionFromDatabase(_value);
    }
    /**
     *
     * @return
     */

    public ArrayList<String> getAllAdmissionYearFromDatabase(){
        ArrayList<String> temp = new ArrayList<>();
        ResultSet rs = studentDAL.getAllAdmissionYearFromDatabase();
        try {
            while(rs.next()){
                temp.add(rs.getString(1));
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return temp;
    }
    
    public ArrayList<StudentDTO> getAllStudentToArray() {
        ArrayList<StudentDTO> temp = new ArrayList<StudentDTO>();
        ResultSet rs = getAllStudent();
        try {
            while (rs.next()) {
                StudentDTO student = new StudentDTO(rs.getNString(1),
                        rs.getNString(2),
                        rs.getNString(3),
                        rs.getDate(4),
                        rs.getNString(5),
                        rs.getNString(6),
                        rs.getNString(7),
                        rs.getInt(8),
                        rs.getNString(9));
                //Add danh sach id doi tuong zo
                student.setM_ListPriorityObject(getListPriorityObjectOfStudent(student.getStudentID()));
                temp.add(student);

            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return temp;
    }

    /**
     * Ham Them mot sinh vien vao database
     *
     * @param _studentObj
     * @return
     */
    public boolean insertStudentToDatabase(StudentDTO _studentObj) {
        return studentDAL.insertStudentToDatabase(_studentObj);
    }

    /**
     * Ham cap nhat lai 1 sinh vien trong database
     *
     * @param _studentObj
     * @return
     */
    public boolean updateStudentToDatabase(StudentDTO _studentObj) {
        return studentDAL.updateStudentToDatabase(_studentObj);
    }

    public boolean deleteStudentFromDatabase(StudentDTO _studentObj) {
        return studentDAL.deleteStudentFromDatabase(_studentObj);
    }

    /**
     * Ham tra ve MSSV lon nhat theo nam nhap hoc
     *
     * @param _input
     * @return
     */
    public int getStudentIdMaxFromDatabase(String _input) {
        return studentDAL.getStudentIdMaxFromDatabase(_input);
    }

    /**
     * Ham tang tu dong mssv khi insert sinh vien
     *
     * @param _namNH
     * @param _idMax
     * @return
     */
    public String initStudentID(String _namNH, int _idMax) {
        String id = "";
        if (_idMax == 0) {
            id += Standard.cutToGetStudentID(_namNH) + "000001";
        } else {
            _idMax++;
            id += _idMax;
        }
        return id;
    }
    
    ///
    public ResultSet GetAllSubjectByRegistrationCoursesID(Object[] _value){
        return studentDAL.GetAllSubjectByRegistrationCoursesID(_value);
    }
    public ResultSet getAllListScoreStudentBySubjectSemesterYear(String _idSubject, String _idYear, String _idSemester) {
        return studentDAL.getAllListScoreStudentBySubjectSemesterYear(_idSubject, _idYear, _idSemester);
    }

    public ArrayList<ScoreStudentDTO> getAllListScoreStudentBySubjectSemesterYearToArray(String _idSubject, String _idYear, String _idSemester) {
        ArrayList<ScoreStudentDTO> arrScoreStudent = new ArrayList<>();
        try {
            ResultSet rs = getAllListScoreStudentBySubjectSemesterYear(_idSubject, _idYear, _idSemester);
            while (rs.next()) {
                ScoreStudentDTO listScoreStudent = new ScoreStudentDTO(rs.getNString(3), rs.getNString(4), rs.getFloat(5), rs.getNString(7), rs.getNString(6), rs.getNString(2));
                arrScoreStudent.add(listScoreStudent);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ListSubjectOpenBLL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return arrScoreStudent;
    }
    
    public StudentDTO getStudentById(String _id)
    {
        StudentDTO temp = null;
        ResultSet rs = studentDAL.getStudentById(_id);
        try {
            while(rs.next())
            {
                temp = new StudentDTO(rs.getNString(1), rs.getNString(2), rs.getNString(3),
                rs.getDate(4), rs.getNString(5), rs.getNString(6),
                rs.getNString(7), rs.getInt(8), rs.getNString(9));
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return temp;
    }
    public StudentDisplayDTO getInfoStudentById(String _id) 
     {
        StudentDisplayDTO temp = null;
        ArrayList<PriorityObjectDTO> listObjPri;
        ResultSet rs = studentDAL.getInfoStudentById(_id);
        try {
            listObjPri = new ArrayList<>();
            while(rs.next())
            {
                temp = new StudentDisplayDTO(rs.getNString(1), 
                        rs.getNString(2), 
                        rs.getNString(3),
                        rs.getDate(4),
                        rs.getNString(5),
                        rs.getNString(6),
                        rs.getNString(7),
                        rs.getNString(8), 
                        rs.getInt(11), 
                        rs.getNString(12),
                        rs.getNString(13));
                //Lay danh sach doi tuong bo vao obj
                PriorityObjectDTO objPriority = new PriorityObjectDTO(rs.getNString(9),rs.getNString(10), rs.getInt(14));
                listObjPri.add(objPriority);
            }
            if(temp != null)
                temp.setListPriorityObject(listObjPri);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return temp;
    }
}

